import { EventCard, Player, GameState } from '../types';

// 机会卡 (Chance Cards)
export const chanceCards: EventCard[] = [
  {
    id: 'chance-1',
    title: '银行分红',
    description: '你的投资获得回报，获得$200',
    type: 'chance',
    effect: (player: Player) => {
      player.money += 200;
    },
  },
  {
    id: 'chance-2',
    title: '税务检查',
    description: '税务局找上门来，支付$150',
    type: 'chance',
    effect: (player: Player) => {
      player.money -= 150;
    },
  },
  {
    id: 'chance-3',
    title: '前进到起点',
    description: '直接前进到起点，领取奖励',
    type: 'chance',
    effect: (player: Player) => {
      player.position = 0;
    },
  },
  {
    id: 'chance-4',
    title: '后退三步',
    description: '你走错了路，后退3格',
    type: 'chance',
    effect: (player: Player) => {
      player.position = (player.position - 3 + 40) % 40;
    },
  },
  {
    id: 'chance-5',
    title: '前进五步',
    description: '你找到了捷径，前进5格',
    type: 'chance',
    effect: (player: Player) => {
      player.position = (player.position + 5) % 40;
    },
  },
  {
    id: 'chance-6',
    title: '房屋维修',
    description: '你的房产需要维修，每栋建筑支付$50',
    type: 'chance',
    effect: (player: Player, game: GameState) => {
      let totalBuildings = 0;
      player.properties.forEach(propId => {
        const prop = game.board.find(s => s.property?.id === propId)?.property;
        if (prop) {
          totalBuildings += prop.level;
        }
      });
      player.money -= totalBuildings * 50;
    },
  },
  {
    id: 'chance-7',
    title: '街道维护',
    description: '城市要求你维护街道，每个地块支付$40',
    type: 'chance',
    effect: (player: Player) => {
      player.money -= player.properties.length * 40;
    },
  },
  {
    id: 'chance-8',
    title: '彩票中奖',
    description: '恭喜！你中了彩票，获得$500',
    type: 'chance',
    effect: (player: Player) => {
      player.money += 500;
    },
  },
  {
    id: 'chance-9',
    title: '慈善捐款',
    description: '你决定做慈善，捐出$100',
    type: 'chance',
    effect: (player: Player) => {
      player.money -= 100;
    },
  },
  {
    id: 'chance-10',
    title: '免租金卡',
    description: '下次支付租金时免费',
    type: 'chance',
    effect: (player: Player) => {
      player.items.push({
        id: 'free-rent',
        name: '免租金卡',
        description: '下次支付租金时免费',
        effect: () => {},
      });
    },
  },
  {
    id: 'chance-11',
    title: '双倍收入',
    description: '下次收取租金时获得双倍',
    type: 'chance',
    effect: (player: Player) => {
      player.items.push({
        id: 'double-rent',
        name: '双倍收入卡',
        description: '下次收取租金时获得双倍',
        effect: () => {},
      });
    },
  },
  {
    id: 'chance-12',
    title: '生日快乐',
    description: '今天是你的生日，每位玩家给你$50',
    type: 'chance',
    effect: (player: Player, game: GameState) => {
      const otherPlayers = game.players.filter(p => p.id !== player.id && !p.isBankrupt);
      otherPlayers.forEach(p => {
        const gift = Math.min(50, p.money);
        p.money -= gift;
        player.money += gift;
      });
    },
  },
  {
    id: 'chance-13',
    title: '医疗费用',
    description: '你生病了，支付医疗费$100',
    type: 'chance',
    effect: (player: Player) => {
      player.money -= 100;
    },
  },
  {
    id: 'chance-14',
    title: '学费退款',
    description: '学校退还学费，获得$150',
    type: 'chance',
    effect: (player: Player) => {
      player.money += 150;
    },
  },
  {
    id: 'chance-15',
    title: '咨询费',
    description: '你提供了专业咨询，获得$250',
    type: 'chance',
    effect: (player: Player) => {
      player.money += 250;
    },
  },
  {
    id: 'chance-16',
    title: '罚款',
    description: '违规停车，支付罚款$75',
    type: 'chance',
    effect: (player: Player) => {
      player.money -= 75;
    },
  },
  {
    id: 'chance-17',
    title: '股票收益',
    description: '你的股票升值，获得$300',
    type: 'chance',
    effect: (player: Player) => {
      player.money += 300;
    },
  },
  {
    id: 'chance-18',
    title: '保险赔付',
    description: '保险公司赔付，获得$200',
    type: 'chance',
    effect: (player: Player) => {
      player.money += 200;
    },
  },
  {
    id: 'chance-19',
    title: '遗产继承',
    description: '你继承了一笔遗产，获得$400',
    type: 'chance',
    effect: (player: Player) => {
      player.money += 400;
    },
  },
  {
    id: 'chance-20',
    title: '投资失败',
    description: '你的投资失败了，损失$200',
    type: 'chance',
    effect: (player: Player) => {
      player.money -= 200;
    },
  },
  {
    id: 'chance-21',
    title: '免费升级',
    description: '免费升级你的一个地块',
    type: 'chance',
    effect: (player: Player, game: GameState) => {
      // Will be handled by UI to let player choose
      player.items.push({
        id: 'free-upgrade',
        name: '免费升级卡',
        description: '免费升级一个地块',
        effect: () => {},
      });
    },
  },
  {
    id: 'chance-22',
    title: '地产税',
    description: '支付地产税，每个地块$30',
    type: 'chance',
    effect: (player: Player) => {
      player.money -= player.properties.length * 30;
    },
  },
  {
    id: 'chance-23',
    title: '商业合作',
    description: '商业合作成功，获得$350',
    type: 'chance',
    effect: (player: Player) => {
      player.money += 350;
    },
  },
  {
    id: 'chance-24',
    title: '法律诉讼',
    description: '卷入法律诉讼，支付$180',
    type: 'chance',
    effect: (player: Player) => {
      player.money -= 180;
    },
  },
  {
    id: 'chance-25',
    title: '顾问费',
    description: '聘请顾问，支付$120',
    type: 'chance',
    effect: (player: Player) => {
      player.money -= 120;
    },
  },
  {
    id: 'chance-26',
    title: '版权收入',
    description: '你的作品获得版权收入，获得$280',
    type: 'chance',
    effect: (player: Player) => {
      player.money += 280;
    },
  },
  {
    id: 'chance-27',
    title: '旅游奖励',
    description: '旅游公司奖励，获得$150',
    type: 'chance',
    effect: (player: Player) => {
      player.money += 150;
    },
  },
  {
    id: 'chance-28',
    title: '设备维护',
    description: '设备需要维护，支付$90',
    type: 'chance',
    effect: (player: Player) => {
      player.money -= 90;
    },
  },
  {
    id: 'chance-29',
    title: '广告收入',
    description: '广告合作收入，获得$220',
    type: 'chance',
    effect: (player: Player) => {
      player.money += 220;
    },
  },
  {
    id: 'chance-30',
    title: '随机传送',
    description: '你被传送到一个随机位置',
    type: 'chance',
    effect: (player: Player) => {
      player.position = Math.floor(Math.random() * 40);
    },
  },
];

// 社区卡 (Community Cards)
export const communityCards: EventCard[] = [
  {
    id: 'community-1',
    title: '社区服务奖',
    description: '你的社区服务获得认可，获得$100',
    type: 'community',
    effect: (player: Player) => {
      player.money += 100;
    },
  },
  {
    id: 'community-2',
    title: '社区费用',
    description: '支付社区管理费$80',
    type: 'community',
    effect: (player: Player) => {
      player.money -= 80;
    },
  },
  {
    id: 'community-3',
    title: '邻里互助',
    description: '邻居帮你修理房屋，节省$120',
    type: 'community',
    effect: (player: Player) => {
      player.money += 120;
    },
  },
  {
    id: 'community-4',
    title: '社区活动',
    description: '组织社区活动，支出$60',
    type: 'community',
    effect: (player: Player) => {
      player.money -= 60;
    },
  },
  {
    id: 'community-5',
    title: '全民分红',
    description: '社区分红，每位玩家获得$100',
    type: 'community',
    effect: (player: Player, game: GameState) => {
      game.players.forEach(p => {
        if (!p.isBankrupt) {
          p.money += 100;
        }
      });
    },
  },
  {
    id: 'community-6',
    title: '公共设施费',
    description: '所有玩家支付公共设施费$50',
    type: 'community',
    effect: (player: Player, game: GameState) => {
      game.players.forEach(p => {
        if (!p.isBankrupt) {
          p.money -= 50;
        }
      });
    },
  },
  {
    id: 'community-7',
    title: '慈善拍卖',
    description: '参加慈善拍卖，支付$150',
    type: 'community',
    effect: (player: Player) => {
      player.money -= 150;
    },
  },
  {
    id: 'community-8',
    title: '社区彩票',
    description: '社区彩票中奖，获得$300',
    type: 'community',
    effect: (player: Player) => {
      player.money += 300;
    },
  },
  {
    id: 'community-9',
    title: '义工奖励',
    description: '义工服务获得奖励$80',
    type: 'community',
    effect: (player: Player) => {
      player.money += 80;
    },
  },
  {
    id: 'community-10',
    title: '环保奖金',
    description: '环保行为获得奖金$150',
    type: 'community',
    effect: (player: Player) => {
      player.money += 150;
    },
  },
  {
    id: 'community-11',
    title: '选择：投资或保守',
    description: '你可以选择投资$100赌一把，或者保守获得$50',
    type: 'community',
    effect: () => {},
    choices: [
      {
        text: '投资$100（50%机会获得$300）',
        effect: (player: Player) => {
          player.money -= 100;
          if (Math.random() < 0.5) {
            player.money += 300;
          }
        },
      },
      {
        text: '保守获得$50',
        effect: (player: Player) => {
          player.money += 50;
        },
      },
    ],
  },
  {
    id: 'community-12',
    title: '资产交换',
    description: '与资产最少的玩家交换一个地块',
    type: 'community',
    effect: (player: Player, game: GameState) => {
      const otherPlayers = game.players.filter(p => p.id !== player.id && !p.isBankrupt && p.properties.length > 0);
      if (otherPlayers.length > 0 && player.properties.length > 0) {
        const poorestPlayer = otherPlayers.reduce((prev, curr) => 
          curr.money < prev.money ? curr : prev
        );
        
        // Swap first property
        const temp = player.properties[0];
        player.properties[0] = poorestPlayer.properties[0];
        poorestPlayer.properties[0] = temp;
      }
    },
  },
  {
    id: 'community-13',
    title: '社区建设',
    description: '参与社区建设，支付$100',
    type: 'community',
    effect: (player: Player) => {
      player.money -= 100;
    },
  },
  {
    id: 'community-14',
    title: '公益基金',
    description: '公益基金分配，获得$180',
    type: 'community',
    effect: (player: Player) => {
      player.money += 180;
    },
  },
  {
    id: 'community-15',
    title: '社区税',
    description: '缴纳社区税$70',
    type: 'community',
    effect: (player: Player) => {
      player.money -= 70;
    },
  },
  {
    id: 'community-16',
    title: '邻里纠纷',
    description: '解决邻里纠纷，支付$90',
    type: 'community',
    effect: (player: Player) => {
      player.money -= 90;
    },
  },
  {
    id: 'community-17',
    title: '社区奖学金',
    description: '获得社区奖学金$200',
    type: 'community',
    effect: (player: Player) => {
      player.money += 200;
    },
  },
  {
    id: 'community-18',
    title: '公共安全费',
    description: '支付公共安全费$110',
    type: 'community',
    effect: (player: Player) => {
      player.money -= 110;
    },
  },
  {
    id: 'community-19',
    title: '社区表彰',
    description: '获得社区表彰奖金$250',
    type: 'community',
    effect: (player: Player) => {
      player.money += 250;
    },
  },
  {
    id: 'community-20',
    title: '公共卫生费',
    description: '支付公共卫生费$85',
    type: 'community',
    effect: (player: Player) => {
      player.money -= 85;
    },
  },
  {
    id: 'community-21',
    title: '文化活动奖',
    description: '文化活动获奖，获得$160',
    type: 'community',
    effect: (player: Player) => {
      player.money += 160;
    },
  },
  {
    id: 'community-22',
    title: '体育赛事费',
    description: '赞助体育赛事，支付$130',
    type: 'community',
    effect: (player: Player) => {
      player.money -= 130;
    },
  },
  {
    id: 'community-23',
    title: '教育基金',
    description: '教育基金奖励$190',
    type: 'community',
    effect: (player: Player) => {
      player.money += 190;
    },
  },
  {
    id: 'community-24',
    title: '环境保护费',
    description: '支付环境保护费$95',
    type: 'community',
    effect: (player: Player) => {
      player.money -= 95;
    },
  },
  {
    id: 'community-25',
    title: '科技创新奖',
    description: '科技创新获奖$280',
    type: 'community',
    effect: (player: Player) => {
      player.money += 280;
    },
  },
  {
    id: 'community-26',
    title: '基础设施费',
    description: '支付基础设施费$140',
    type: 'community',
    effect: (player: Player) => {
      player.money -= 140;
    },
  },
  {
    id: 'community-27',
    title: '艺术赞助奖',
    description: '艺术赞助获奖$210',
    type: 'community',
    effect: (player: Player) => {
      player.money += 210;
    },
  },
  {
    id: 'community-28',
    title: '交通管理费',
    description: '支付交通管理费$105',
    type: 'community',
    effect: (player: Player) => {
      player.money -= 105;
    },
  },
  {
    id: 'community-29',
    title: '创业扶持金',
    description: '获得创业扶持金$320',
    type: 'community',
    effect: (player: Player) => {
      player.money += 320;
    },
  },
  {
    id: 'community-30',
    title: '全员重置',
    description: '所有玩家回到起点',
    type: 'community',
    effect: (player: Player, game: GameState) => {
      game.players.forEach(p => {
        if (!p.isBankrupt) {
          p.position = 0;
        }
      });
    },
  },
];

// 洗牌函数
export function shuffleCards<T>(cards: T[]): T[] {
  const shuffled = [...cards];
  for (let i = shuffled.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
  }
  return shuffled;
}

// 获取洗牌后的机会卡
export function getShuffledChanceCards(): EventCard[] {
  return shuffleCards(chanceCards);
}

// 获取洗牌后的社区卡
export function getShuffledCommunityCards(): EventCard[] {
  return shuffleCards(communityCards);
}
